const app = getApp();
import {debounce} from "../../../utils/util";

const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    active: 0,
    value: 1,
    infoData: [],
    chilId_info: null,
    childId: "",
    timer: '',
    is_open_batch: false,
    valid_number: 1
  },
  // 页面载入时
  onLoad(e) {},
  // 页面初始化
  init(e) {},
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    console.log('ddddddddddd')
    this.initData();
    this.getData();
  },
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {},
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {},
  tabChange(e) {
    this.setData({
      active: e.detail.index
    })
    this.initData();
    if (this.data.active != 1) {
      this.getData();
    }
  },
  initData() {
    this.setData({
      chilId_info: null,
      childId: "",
      infoData: {},
      value: 1,
    })
  },
  changeGet(e) {
    this.setData({
      value: e.detail
    })
    this.getData();
  },
  inpChild(e) {
    let val = e.detail.value;
    this.setData({
      childId: val
    })
    // 防抖
    if (this.data.timer) clearTimeout(this.data.timer);
    this.data.timer = setTimeout(() => {
      debounce(this.getChild());
    }, 600);
  },
  async getChild() {
    if (!this.data.childId) return
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.checkChildId"),
      data: {
        child_id: this.data.childId
      }
    })
    if (data.result) {
      this.setData({
        chilId_info: data.data,
      })
    } else {
      this.setData({
        chilId_info: null
      })
    }
  },
  async getData() {
    let json = {
      count: this.data.value
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child-id.checkChildIds"),
      data: json,
    })
    if (data.result) {
      this.setData({
        infoData: data.data.data,
        is_open_batch: data.data.is_open_batch
      })
      if (this.data.infoData) {
        if (this.data.infoData?.valid_number && this.data.is_open_batch) {
          this.setData({
            valid_number: this.data.infoData?.valid_number
          })
        }
      }
    } else {
      app.tips(data.msg);
      this.data.infoData = {};
      this.setData({
        infoData: {},
        is_open_batch: false
      })
    }
  },
  async tobuy() {
    if(this.data.active == 0 && !this.data.infoData.hasOwnProperty('price')){
      console.log('平台购买没有数据return')
      return;
    }else if (this.data.active == 1 && !this.data.chilId_info.hasOwnProperty('child_id')){
      console.log('挂售购买没有数据return')
      return;
    }
    let {
      pay_sn
    } = await this.getOrderSn();
    let _seatChildId_json = {
      pay_sn: pay_sn
    };
    if (this.data.active == 0) {
      _seatChildId_json.count = this.data.value;
    }
    if (this.data.active == 1) {
      _seatChildId_json.child_id = this.data.chilId_info.child_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child_id.seatChildId"),
      data:_seatChildId_json
    })
    if(!data.result){
      app.tips(data.msg);
      return
    }
    if(this.data.active == 1){
      if(this.data.chilId_info && this.data.chilId_info.hasOwnProperty('child_id')){
        wx.navigateTo({
          url: `/packageD/buy/orderPay/orderPay?tag=idShopApply&price=${Number(data.data.price)}&child_id=${this.data.chilId_info.child_id}&ordersn=${pay_sn}`,
        })
        return
      }
    }
    wx.navigateTo({
      url: `/packageD/buy/orderPay/orderPay?tag=idShopSub&price=${Number(data.data.price)}&count=${this.data.value}&ordersn=${pay_sn}&actualCount=${data.data.count}`,
    })
    // if (this.data.active == 1) {
    //   if (this.data.chilId_info?.child_id) {
        
    //     wx.navigateTo({
    //       url: `/packageD/buy/orderPay/orderPay?tag=idShopApply&price=${Number(this.data.chilId_info.price)}&child_id=${this.data.chilId_info.child_id}&ordersn=${pay_sn}`,
    //     })
    //   }
    //   return
    // } else {
    //   if (!this.data.infoData?.price) {
    //     return
    //   }
    //   let {
    //     pay_sn
    //   } = await this.getOrderSn();
    //   wx.navigateTo({
    //     url: `/packageD/buy/orderPay/orderPay?tag=idShopSub&price=${Number(this.data.infoData.price)}&count=${this.data.value}&ordersn=${pay_sn}`,
    //   })
    // }
  },
  async getOrderSn() {
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child-id.getPaySn")
    })
    return data.data
  }
}

Page(pageOptions)